package com.changan.controller;

import com.changan.model.Order;
import com.changan.model.Product;
import com.changan.service.OrderService;
import com.changan.service.ProductService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.UUID;

/**
 * @program: alipay
 * @description: 订单控制器
 * @author: Mr.Nie
 * @create: 2021-12-10 12:10
 **/
public class OrderController {

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderService orderService;

    @RequestMapping("/createOrder")
    public String createOrder(String productId, Integer buyCount, Model model){
        Product product = productService.getProductById(productId);
        Order order = new Order();
        String uuid = UUID.randomUUID().toString();
        order.setId(uuid);
        order.setOrderNo(uuid);
        order.setCreateTime(new Date());
        order.setBuyCount(buyCount);
        order.setOrderAmount(String.valueOf(Float.valueOf(product.getPrice()) * order.getBuyCount()));
        order.setOrderStatus("未支付");
        order.setProductId(productId);
        orderService.insert(order);
        model.addAttribute("order",order);
        return "goPay";
    }
}
